﻿//--------------XMLHttpRequest---------------------// 
  function GetXmlHttpObject(){
   var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
  }
//--------------用户名检测---------------------//
  function Ajax(data){	
	   
	    xmlhttp=GetXmlHttpObject();
		xmlhttp.open("POST","checkRegisterServlet?userLoginName="+data,true);
		xmlhttp.send(null);
	
	    document.getElementById('userLoginName_notice').innerHTML = process_request;//显示状态
	    
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4){
				if (xmlhttp.status==200){
				var responseText = xmlhttp.responseText;
				   if (responseText=="true" ){
				     ck_user("true");
				      }
				   else{
				   ck_user("false");
				   }
				}else{
					alert("发生错误!");
				}
			}
		}
	}
	
	function chkUserName(obj){
	
		
	     if (checks(obj.value)== false)
		  {
			obj.className = "FrameDivWarn";
			showInfo("userLoginName_notice",msg_un_format);
            change_submit("true");
		  }
		else if(obj.value.length<1){
			obj.className = "FrameDivWarn";
			showInfo("userLoginName_notice",msg_un_blank);
            change_submit("true");
		}

		else if(obj.value.length<3){
			obj.className = "FrameDivWarn";
			showInfo("userLoginName_notice",username_shorter);
            change_submit("true");
		}
		else{
			//调用Ajax函数,向服务器端发送查询
			
			Ajax(obj.value);
			
		}			

	}

function ck_user(result)
{
  if ( result == "true" )
  {  
	 
    document.getElementById('userLoginName_notice').className = "FrameDivWarn";
	showInfo("userLoginName_notice",msg_un_registered);
    change_submit("true");//禁用提交按钮
  }
  else
  { 
    document.getElementById('userLoginName_notice').className = "FrameDivPass";
	showInfo("userLoginName_notice",msg_can_rg);
    change_submit("false");//可用提交按钮
  }
}

function checks(t){
    szMsg="[#%&'\",;:=!^@]";
     //alertStr="";
    for(i=1;i<szMsg.length+1;i++){
     if(t.indexOf(szMsg.substring(i-1,i))>-1){
      //alertStr="请勿包含非法字符如[#_%&'\",;:=!^]";
      return false;
     }
    }
    return true;
   }
//-----------EMAIL检测--------------------------------//   
function Ajaxemail(data){	
	xmlhttp=GetXmlHttpObject();
	xmlhttp.open("POST","checkEmailServlet?userEmail="+data,true);
	xmlhttp.send(null);
    document.getElementById('email_notice').innerHTML = process_request;//显示状态
    xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4){
			if (xmlhttp.status==200){
			var responseText = xmlhttp.responseText;
			
			   if (responseText=="true" ){
				   ck_email("true");
				   
			      }
			   else{
				   ck_email("false");
			   }
			}else{
				alert("发生错误!");
			}
		}
	}
}
function checkEmail(obj)
{
   if(obj.value.length<1){
		obj.className = "FrameDivWarn";
		showInfo("email_notice",msg_email_blank);
        change_submit("true");
	}
   else if (chekemail(obj.value)==false)

  {
	   obj.className = "FrameDivWarn";
	showInfo("email_notice",msg_email_format);
	change_submit("true");  
 } 
 
 else
   {
	 Ajaxemail(obj.value);
   }
}

function chekemail(email) {  
 var pattern = /^[a-za-z0-9_-]+@[a-za-z0-9_-]+(\.[a-za-z0-9_-]+)+$/;  
 if(pattern.test(email)) {  
  return true;  
 }  
 else {  
  return false;  
 }  
}

function ck_email(result)
{
  if ( result == "true" )
  {  
    document.getElementById('email_notice').className = "FrameDivWarn";
	showInfo("email_notice",msg_email_registered);
    change_submit("true");//禁用提交按钮
  }
  else
  { 
    document.getElementById('email_notice').className = "FrameDivPass";
	showInfo("email_notice",msg_can_rg);
    change_submit("false");//可用提交按钮
  }
}

//-----------TEL检测--------------------------------// 
function checkTel(Tel)
{
 if (chektel(Tel.value)==false)

  {
    Tel.className = "FrameDivWarn";
	showInfo("tel_notice",msg_tel_format);
	change_submit("true");  
 } 
 
else
   {
   showInfo("tel_notice",info_right);
   Tel.className = "FrameDivPass";
   change_submit("false"); 
   }
}

function chektel(Tel) {  
 var pattern = /(^(1[3|4|5|8][0-9]\d{8}$)|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/;
 if(pattern.test(Tel)) {  
  return true;  
 }  
 else {  
  return false;  
 }  
} 

//--------------------原始密码检测-----------------------------//
function Ajaxpassword(data){	
	xmlhttp=GetXmlHttpObject();
	xmlhttp.open("POST","checkPwdServlet?userOldPwd="+data,true);
	xmlhttp.send(null);
    document.getElementById('password_notice1').innerHTML = process_request;//显示状态
    xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4){
			if (xmlhttp.status==200){
			var responseText = xmlhttp.responseText;

			   if (responseText=="true" ){
				   ck_pwd("true");
			      }
			   else{
				   ck_pwd("false");
			   }
			}else{
				alert("发生错误!");
			}
		}
	}
}
function checkpwd(obj)
{
   if(obj.value.length<1){
		obj.className = "FrameDivWarn";
		showInfo("password_notice1",password_empty );
        change_submit("true");
	}
 else
   {
	 Ajaxpassword(obj.value);
   }
}

function ck_pwd(result)
{
  if ( result == "false" )
  {  
    document.getElementById('userOldPwd').className = "FrameDivWarn";
	showInfo("password_notice1",confirm_pwd_invalid);
    change_submit("true");//禁用提交按钮
  }
  else
  { 
    document.getElementById('userOldPwd').className = "FrameDivPass";
	showInfo("password_notice1",msg_can_md);
    change_submit("false");//可用提交按钮
  }
}

//--------------------密码检测-----------------------------//
function check_password( password )
{
    if ( password.value.length < 6 )
    {
		showInfo("password_notice",password_shorter_s);
		password.className = "FrameDivWarn";
		change_submit("true");//禁用提交按钮
    }
	else if(password.value.length > 30){
		showInfo("password_notice",password_shorter_m);
		password.className = "FrameDivWarn";
		change_submit("true");//禁用提交按钮
		}
    else
    {
		showInfo("password_notice",info_right);
		password.className = "FrameDivPass";
		change_submit("false");//允许提交按钮
    }
}

function check_conform_password( conform_password )
{
	
    var password = document.form1.userPwd.value;
    if ( conform_password.value.length < 6 )
    {
		showInfo("conform_password_notice",password_shorter_s);
		conform_password.className = "FrameDivWarn";
		change_submit("true");//禁用提交按
        return false;
    }
    if ( conform_password.value!= password)
    {
		showInfo("conform_password_notice",confirm_password_invalid);
		conform_password.className = "FrameDivWarn";
		change_submit("true");//禁用提交按
		document.form2.userPwd.focus();
		return false;
    }
    else
    {   
    	
	    conform_password.className = "FrameDivPass";
		showInfo("conform_password_notice",info_right);
		change_submit("false");//允许提交按钮
		return true;
    }
}

function checkReg(){
    var LoginName=document.getElementById("userLoginName_notice").innerHTML;

    var email_notice=document.getElementById("email_notice").innerHTML;
    var l1 = LoginName.toString();
    var l2 = email_notice.toString();
    var reg = /存在/g; 
	 var name=document.form1.userLoginName.value;
	 var userPwd=document.form1.userPwd.value;
	 var userRePwd=document.form1.userRePwd.value;
	 var userEmail=document.form1.userEmail.value;
	 var userTelephone=document.form1.userTelephone.value;
	  var pattern = /(^(1[3|4|5|8][0-9]\d{8}$)|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/;

	  if(l1.match(reg))
	{
	
	document.form1.userLoginName.focus();
	  return false;
	 }
	  if(l2.match(reg))
	{
	
	document.form1.userEmail.focus();
	  return false;
	 }
	 if(name.length==0){
		document.form1.userLoginName.focus();
		 return false;
		 
	 }
	 if(userPwd.length==0){
		document.form1.userPwd.focus();
		 return false;
		 
	 }
	  if(userRePwd.length==0){
		document.form1.userRePwd.focus();
		 return false;
		 
	 }
	  if(userEmail.length==0){
		document.form1.userEmail.focus();
		
		 return false;
		 
	 }
	  if(!userTelephone.match(pattern)){
		  document.form1.userTelephone.focus();
		 return false;
		  
	  }
	  if(document.form1.agreement.checked == false){
		  showInfo("agreement_notice",agreement);
		  return false;
	  }
	  if(userRePwd.length<6||userPwd.length<6){
		  document.form1.userPwd.focus();
				return false;
			}else if(userPwd!=userRePwd){
				document.form1.userRePwd.focus();
				document.getElementById('conform_password_notice').innerHTML="<font color='red'>两次输入密码不相同!</font>";
				return false;
			}else{
				return true;
			}
}
		function check(){
			var password = document.form2.userPwd.value;
			var repassword = document.form2.userRePwd.value;
			if(password.length<6||repassword.length<6){
				return false;
			}else if(password!=repassword){
				return false;
			}else{
				return true;
			}
		}
//* *--------------------检测密码强度-----------------------------* *//

function checkIntensity(pwd)
{
  var Mcolor = "#FFF",Lcolor = "#FFF",Hcolor = "#FFF";
  var m=0;

  var Modes = 0;
  for (i=0; i<pwd.length; i++)
  {
    var charType = 0;
    var t = pwd.charCodeAt(i);
    if (t>=48 && t <=57)
    {
      charType = 1;
    }
    else if (t>=65 && t <=90)
    {
      charType = 2;
    }
    else if (t>=97 && t <=122)
      charType = 4;
    else
      charType = 4;
    Modes |= charType;
  }

  for (i=0;i<4;i++)
  {
    if (Modes & 1) m++;
      Modes>>>=1;
  }

  if (pwd.length<=4)
  {
    m = 1;
  }

  switch(m)
  {
    case 1 :
      Lcolor = "2px solid red";
      Mcolor = Hcolor = "2px solid #DADADA";
    break;
    case 2 :
      Mcolor = "2px solid #f90";
      Lcolor = Hcolor = "2px solid #DADADA";
    break;
    case 3 :
      Hcolor = "2px solid #3c0";
      Lcolor = Mcolor = "2px solid #DADADA";
    break;
    case 4 :
      Hcolor = "2px solid #3c0";
      Lcolor = Mcolor = "2px solid #DADADA";
    break;
    default :
      Hcolor = Mcolor = Lcolor = "";
    break;
  }
  document.getElementById("pwd_lower").style.borderBottom  = Lcolor;
  document.getElementById("pwd_middle").style.borderBottom = Mcolor;
  document.getElementById("pwd_high").style.borderBottom   = Hcolor;

}
//--------------注册协议状态检测---------------------//
function check_agreement(){
   if (document.form1.agreement.checked == false)
  {
	showInfo("agreement_notice",agreement);
     change_submit("true");//禁用提交按钮 
  }
  else
  {
     showInfo("agreement_notice",info_right);
	 change_submit("false");//允许提交 
	}
}


//-------------处理注册程序-----------------------------//

function register() {

	if(document.form1.userLoginName.value=="")
	{
	
	  document.form1.userLoginName.focus();
	  return false;
	 }
 else if(document.form1.userPwd.value=="")
	{
	
	document.form1.userPwd.focus();
	  return false;
	 }
 else if(document.form1.userRePwd.value=="")
	{
	
	document.form1.userRePwd.focus();
	  return false;
	 }
 else if(document.form1.userEmail.value=="")
	{
	 
	  document.form1.userEmail.focus();
	  return false;
	 }	
 else if(document.form1.agreement.checked == false)
	{
	
    document.form1.agreement.focus();
	  return false;
	 }
 else if(document.form1.userPwd.value=="")
	{
	
	document.form1.userPwd.focus();
	  return false;
	 }

return window.confirm("你真的要修改信息吗？"); 	
}
//-----------验证码检测--------------------------------//   
function Ajaxcode(data){	
	xmlhttp=GetXmlHttpObject();
	xmlhttp.open("POST","SendCodeServlet?ckcode="+data,true);
	xmlhttp.send(null);
    document.getElementById('code_notice').innerHTML = process_request;//显示状态
    xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4){
			if (xmlhttp.status==200){
			var responseText = xmlhttp.responseText;
			   if (responseText=="true" ){
				   ck_code("true");
			      }
			   else{
				   ck_code("false");
			   }
			}else{
				alert("发生错误!");
			}
		}
	}
}
function checkcode(obj)
{
   if(obj.value.length<1){
		obj.className = "FrameDivWarn";
		showInfo("code_notice",msg_code_blank);
        change_submit("true");
	}
  else
   {
	  Ajaxcode(obj.value);
   }
}

function ck_code(result)
{
  if ( result == "false" )
  {  
    document.getElementById('ckcode').className = "FrameDivWarn";
	showInfo("code_notice",msg_code_wrong);
    change_submit("true");//禁用提交按钮
  }
  else
  { 
    document.getElementById('ckcode').className = "FrameDivPass";
	showInfo("code_notice",msg_code_right);
    change_submit("false");//可用提交按钮
  }
}
//------------ 按钮状态设置-----------------------------//
function change_submit(zt)
{ 
	
	
     if (zt == "true")
     {
         document.forms['form1'].elements['sure'].disabled = 'disabled';
    	
     }
   else
     {
	
	   document.forms['form1'].elements['sure'].disabled = '';
	  } 
    
}
//------公用程序------------------------------------//
	function showInfo(target,Infos){
    document.getElementById(target).innerHTML = Infos;
	}
	function showclass(target,Infos){
    document.getElementById(target).className = Infos;
	}	
var process_request = "<img src='/NCP/images/register/waitting.gif' width='16' height='16' border='0' align='absmiddle'>数据处理中...";
var username_empty = "<span style='COLOR:#ff0000'>  <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 用户名不能为空!</span>";
var username_shorter = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 用户名长度不能少于 3 个字符。</span>";
var username_invalid = "- 用户名只能是由字母数字以及下划线组成。";
var password_empty = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 登录密码不能为空。</span>";
var password_shorter_s = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 登录密码不能少于 6 个字符。</span>";
var password_shorter_m = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 登录密码不能多于 30 个字符。</span>";
var confirm_password_invalid = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 两次输入密码不一致!</span>";
var confirm_pwd_invalid = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 与初始密码不一致!</span>";
var email_empty = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> Email 不能为空</span>";
var email_invalid = "- Email 不是合法的地址";
var agreement = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 您没有接受协议</span>";
var msg_un_blank = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' />用户名不能为空!</span>";
var msg_un_length = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 用户名最长不得超过15个字符</span>";
var msg_un_format = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 用户名含有非法字符!</span>";
var msg_un_registered = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 用户名已经存在,请重新输入!</span>";
var msg_code_right = "<span style='COLOR:#006600'> <img src='/NCP/images/register/yes.gif' width='16' height='16' border='0' align='absmiddle' /> 验证码正确！</span>";
var msg_code_wrong="<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 验证码不正确!</span>";
var msg_can_rg = "<span style='COLOR:#006600'> <img src='/NCP/images/register/yes.gif' width='16' height='16' border='0' align='absmiddle' /> 可以注册！</span>";
var msg_can_md = "<span style='COLOR:#006600'> <img src='/NCP/images/register/yes.gif' width='16' height='16' border='0' align='absmiddle' />可以修改了！</span>";
var msg_email_blank = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 邮件地址不能为空!</span>";
var msg_code_blank = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 验证码不能为空!</span>";
var msg_email_registered = " <span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 邮箱已存在,请重新输入!</span>";
var msg_email_format = "<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 邮件地址不合法!</span>";
var msg_tel_format="<span style='COLOR:#ff0000'> <img src='/NCP/images/register/no.gif' width='16' height='16' border='0' align='absmiddle' /> 联系电话格式不正确!</span>";
var username_exist = "用户名 %s 已经存在";
var info_can="<span style='COLOR:#006600'> <img src='/NCP/images/register/yes.gif' width='16' height='16' border='0' align='absmiddle' /> 可以注册!</span>";
var info_right="<span style='COLOR:#006600'> <img src='/NCP/images/register/yes.gif' width='16' height='16' border='0' align='absmiddle' /> 填写正确!</span>";	